package com.atguigu.java1;

import org.junit.Test;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 *      实现TCP的网络编程
 *          例3：从客户端发送文件给服务端，服务端保存到本地。并返回“发送成功”给客户端。并关闭相应的连接
 *
 *
 * @author : leimingchao
 * @date Date : 2021年09月05日 0:51
 */
public class TCPTest3 {

    @Test
    public void client() {

        Socket socket = null;
        OutputStream outputStream = null;
        FileInputStream fileInputStream = null;
        InputStream inputStream = null;
        ByteArrayOutputStream byteArrayOutputStream = null;
        try {
            InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
            socket = new Socket(inetAddress,9090);

            outputStream = socket.getOutputStream();

            fileInputStream = new FileInputStream(new File("Koala.jpg"));

            byte[] buffer = new byte[1024];
            int len;
            while ((len = fileInputStream.read(buffer)) != -1) {
                outputStream.write(buffer,0,len);
            }

            //关闭数据的输出
            socket.shutdownOutput();

            //接收来自服务端的数据，并显示在控制台上
            inputStream = socket.getInputStream();
            byteArrayOutputStream = new ByteArrayOutputStream();

            byte[] byteArray = new byte[20];
            int length;
            while ((length = inputStream.read(byteArray)) != -1) {
                byteArrayOutputStream.write(byteArray,0,length);
            }
            System.out.println(byteArrayOutputStream);


        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            if (byteArrayOutputStream != null) {
                try {
                    byteArrayOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (fileInputStream != null) {

                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null) {

                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {

                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    @Test
    public void server() {

        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream inputStream = null;
        FileOutputStream fileOutputStream = null;
        OutputStream outputStream = null;
        try {
            serverSocket = new ServerSocket(9090);

            socket = serverSocket.accept();
            inputStream = socket.getInputStream();

            fileOutputStream = new FileOutputStream(new File("Koala2.jpg"));

            byte[] buffer = new byte[1024];

            int len;
            while ((len = inputStream.read(buffer)) !=  -1) {
                fileOutputStream.write(buffer,0,len);
            }
            System.out.println("图片传输完成");

            //服务端给予客户端反馈
            outputStream = socket.getOutputStream();
            outputStream.write("你好，美女，照片我已收到，非常漂亮！".getBytes());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            if (outputStream != null) {

                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (fileOutputStream != null) {

                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream != null) {

                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {

                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (serverSocket != null) {

                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }


    }
}
